using Crux.Core.Payloads;

namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// PayloadExtensions
/// </summary>
public static class PayloadExtensions
{
    /// <summary>
    /// 成功
    /// </summary>
    /// <param name="controller"></param>
    /// <returns></returns>
    public static OkObjectResult Success(this Controller controller)
        => controller.Ok(Payload.Success());
    /// <summary>
    /// 成功返回返回
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="controller"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static OkObjectResult Success<T>(this Controller controller, T data)
        => controller.Ok(Payload<T>.Success(data));
    /// <summary>
    ///
    /// </summary>
    /// <param name="controller"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static BadRequestObjectResult Error(this Controller controller, string message)
        => controller.BadRequest(Payload.Error(message));
    /// <summary>
    ///
    /// </summary>
    /// <param name="controller"></param>
    /// <param name="persistId"></param>
    /// <returns></returns>
    public static CreatedResult PersistSuccess(this Controller controller, string persistId)
        => controller.Created($"{controller.Request.Path}/{persistId}", Payload<string>.Success(persistId));
    /// <summary>
    ///
    /// </summary>
    /// <param name="controller"></param>
    /// <param name="localtion"></param>
    /// <returns></returns>
    public static AcceptedResult AsyncSuccess(this Controller controller, string? localtion = null)
        => controller.Accepted(localtion ?? controller.Request.Path, Payload.Success());
}